﻿using HZH_Controls.Controls;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using 渐变.customercolumn;

namespace 渐变.user_Form
{
    public partial class user_carFrm : Form
    {
        public user_carFrm()
        {
            InitializeComponent();
        }

        private void ucDataGridView1_Load(object sender, EventArgs e)
        {
            loadSearchType();

            InitDataGridViewHeader();

            var countsql = new StringBuilder();
            countsql.Append($"Select COUNT(ci.CId)");
            countsql.Append(Environment.NewLine);
            countsql.Append($"From CarInfo as ci");
            countsql.Append(Environment.NewLine);
            countsql.Append($"LEFT JOIN CarTypeInfo as cti ON ci.CTypeId = cti.CTypeId");
            countsql.Append(Environment.NewLine);
            countsql.Append($"WHERE ci.CIsDelete = 0");

            int count = (int)DBHelper.ExecuteScalar(countsql.ToString());

            var ds = new List<object>();
            if (count > 0)
            {
                for (int i = 0; i < count; i++)
                {
                    ds.Add(i);
                }
            }
            this.pc1.DataSource = ds;
            this.pc1.PageSize = 10;
            this.pc1.StartIndex = 0;
            this.pc1.FirstPage();
            this.pc1.PageCount = count / 10+(count%10>0?1:0);
            this.pc1.ShowSourceChanged += Pc1_ShowSourceChanged;


            var sql = new StringBuilder();
            sql.Append($"Select top 10 temp.CId, temp.CName,temp.CName,temp.CPrice,temp.CImg,temp.CNumber,temp.CTypeId,temp.CStatus,temp.CTitle");
            sql.Append(Environment.NewLine);
            sql.Append($"from (select row_number() over(order by CID asc) as rownumber,CId,CName,CPrice,CImg,CNumber,ci.CTypeId,CStatus,CTitle From CarInfo as ci");
            sql.Append(Environment.NewLine);
            sql.Append($"LEFT JOIN CarTypeInfo as cti ON ci.CTypeId = cti.CTypeId WHERE ci.CIsDelete = 0) as temp");
            sql.Append(Environment.NewLine);
            sql.Append($"WHERE rownumber > 0;");

            //            select top 10 *
            //from(select row_number()
            //over(order by sno asc) as rownumber, *
            //from student) temp_row
            //where rownumber > 10;

            DataTable dt = new DataTable();
            dt = DBHelper.ExecuteQuery(sql.ToString());
            this.cardata.DataSource = dt;
            this.cardata.First();
        }

        private void InitDataGridViewHeader()
        {
            List<DataGridViewColumnEntity> lstCulumns = new List<DataGridViewColumnEntity>();
            lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "CId", HeadText = "id", Width = 80, WidthType = SizeType.Absolute });
            lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "CName", HeadText = "车名", Width = 120, WidthType = SizeType.Absolute });
            lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "CPrice", HeadText = "日租价格", Width = 120, WidthType = SizeType.Absolute });
            //lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "CImg", HeadText = "图片", Width = 20, WidthType = SizeType.Percent });
            lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "CNumber", HeadText = "车牌号", Width = 150, WidthType = SizeType.Absolute });
            lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "CStatus", HeadText = "状态", Width = 150, WidthType = SizeType.Absolute,Format = (a)=> { return (bool)a ? "启用" : "停用";  } });
            lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "CTitle", HeadText = "类型", Width = 150, WidthType = SizeType.Absolute });
            lstCulumns.Add(new DataGridViewColumnEntity() { HeadText = "图片", Width = 50, WidthType = SizeType.Absolute, CustomCellType = typeof(GridImageCell) });
            this.cardata.Columns = lstCulumns;
            this.cardata.IsShowCheckBox = true;
            List<object> lstSource = new List<object>();
        }

        private void Pc1_ShowSourceChanged(object currentSource)
        {
            var list = this.pc1.GetCurrentSource();
            if (list != null && list.Count > 0)
            {

                var sql = new StringBuilder();
                sql.Append($"Select top 10 temp.CId, temp.CName,temp.CName,temp.CPrice,temp.CImg,temp.CNumber,temp.CTypeId,temp.CStatus,temp.CTitle");
                sql.Append(Environment.NewLine);
                sql.Append($"from (select row_number() over(order by CID asc) as rownumber,CId,CName,CPrice,CImg,CNumber,ci.CTypeId,CStatus,CTitle From CarInfo as ci");
                sql.Append(Environment.NewLine);
                sql.Append($"LEFT JOIN CarTypeInfo as cti ON ci.CTypeId = cti.CTypeId WHERE ci.CIsDelete = 0) as temp");
                sql.Append(Environment.NewLine);
                sql.Append($"WHERE rownumber > {10 * ((int)list[0] / 10)};");

                DataTable dt = new DataTable();
                dt = DBHelper.ExecuteQuery(sql.ToString());
                InitDataGridViewHeader();
                this.cardata.DataSource = dt;
            }
            else
            {
                this.cardata.DataSource = null;
            }
        }
        public void loadSearchType()
        {
            string sql = "select * from CarTypeInfo where CIsDelete = 0";
            DataTable dt = new DataTable();
            dt = DBHelper.ExecuteQuery(sql);
            DataRow dr = dt.NewRow();
            dr[0] = 0;
            dr[1] = "全部分类";
            dr[2] = 0;
            dt.Rows.InsertAt(dr, 0);
            cmbSearchType.DataSource = dt;
            cmbSearchType.DisplayMember = "CTitle";
            cmbSearchType.ValueMember = "CTypeId";

            cmbSearchType.SelectedIndex = 0;

        }
        private void cmbSearchType_TextChanged(object sender, EventArgs e)
        {
            loadSearch();
        }

        /*搜索*/
        public void loadSearch()
        {
            string sql = "select CId,CName,CPrice,CImg,CNumber,CarInfo.CTypeId,CStatus,CarTypeInfo.CTypeId,CTitle from CarInfo,CarTypeInfo where CarInfo.CTypeId=CarTypeInfo.CTypeId and CarInfo.CIsDelete=0 and CTitle=@ctitle";
            SqlParameter type = new SqlParameter("@ctitle", cmbSearchType.Text);
            if (cmbSearchType.Text == "全部分类")
            {
                ucDataGridView1_Load(null, null);
            }
            else
            {
                DataTable dt = new DataTable();
                dt = DBHelper.ExecuteQuery(sql, type);
                //cardata.AutoGenerateColumns = false;
                cardata.DataSource = dt;
            }


        }
        public string operName { get; set; }
        private void btnRent_Click(object sender, EventArgs e)
        {
            string CStatus = (cardata.SelectRow.DataSource as DataRow)["CStatus"].ToString();
            string carName = (cardata.SelectRow.DataSource as DataRow)["CName"].ToString();
            string price = (cardata.SelectRow.DataSource as DataRow)["CPrice"].ToString();
            string carNumber = (cardata.SelectRow.DataSource as DataRow)["CNumber"].ToString();
            if(CStatus == "True")
            {
                MessageBox.Show("可以出租");
                user_rentCarForm rentCarForm = new user_rentCarForm();
                rentCarForm.carNumber = carNumber;
                rentCarForm.price = price;
                rentCarForm.carName = carName;
                rentCarForm.opername = this.operName;
                DialogResult dir = rentCarForm.ShowDialog();
                /* rentCarForm.Show();*/
                if (dir == DialogResult.OK)
                {
                    //loadMsg();
                    ucDataGridView1_Load(null, null);
                }
            }
            else
            {
                this.ShowErrorTip("此车已被租走！请更换车辆！");
            }
        }

        private void user_carFrm_Load(object sender, EventArgs e)
        {
            this.Dock = DockStyle.Fill;
        }

        private void cmbSearchType_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}
